//
//  HotTableViewCell.m
//  Comic
//
//  Created by 程锦 on 16/3/11.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "HotTableViewCell.h"
#import "DetailsCollectionViewCell.h"
#import "ActivityCollectionViewCell.h"
#import "DetailViewController.h"
#import "ComiContentViewController.h"

@interface HotTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation HotTableViewCell

- (void)awakeFromNib {
    
    self.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    NSNumber *zero = [NSNumber numberWithFloat:0];
    
    if ([self.type isEqualToNumber:zero] || [self.name isEqualToString:@"所有作品"]) {
        
        flowLayout.itemSize = CGSizeMake(self.detailsCollectionView.frame.size.width / 3 , self.detailsCollectionView.frame.size.height );
        
    }else{
        
        flowLayout.itemSize = CGSizeMake(self.detailsCollectionView.frame.size.width / 3 + 20, self.detailsCollectionView.frame.size.height );
    
    }
    
    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // 列间距
    flowLayout.minimumLineSpacing = 10;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [self.detailsCollectionView setCollectionViewLayout:flowLayout];
    
    // 注册
    UINib *cellNib = [UINib nibWithNibName:NSStringFromClass([DetailsCollectionViewCell class]) bundle:nil];
    
    [self.detailsCollectionView registerNib:cellNib forCellWithReuseIdentifier:NSStringFromClass([DetailsCollectionViewCell class])];
    
    UINib *activityNib = [UINib nibWithNibName:NSStringFromClass([ActivityCollectionViewCell class]) bundle:nil];
    
    [self.detailsCollectionView registerNib:activityNib forCellWithReuseIdentifier:NSStringFromClass([ActivityCollectionViewCell class])];
    
    // 取消翻页条
    self.detailsCollectionView.showsHorizontalScrollIndicator = NO;
    
    // 签协议
    self.detailsCollectionView.dataSource = self;

    self.detailsCollectionView.delegate =self;
    
    

}

// 设置collectionView等屏幕
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSNumber *zero = [NSNumber numberWithFloat:0];
    
    if ([self.type isEqualToNumber:zero] || [self.name isEqualToString:@"所有作品"]) {
    
        return CGSizeMake(self.detailsCollectionView.frame.size.width / 3 , self.detailsCollectionView.frame.size.height );
        
    }else{
    
        return CGSizeMake(self.detailsCollectionView.frame.size.width / 3 + 20, self.detailsCollectionView.frame.size.height );
    
    }

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSNumber *zero = [NSNumber numberWithFloat:0];
    
    if ([self.type isEqualToNumber:zero] || [self.name isEqualToString:@"所有作品"]) {
    
        return self.hotArr.count;
        
    }else{
    
        return self.comicsArr.count;
    
    }
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        
    self.titleName.text = self.name;
    
    NSNumber *zero = [NSNumber numberWithFloat:0];
    
    if ([self.type isEqualToNumber:zero] || [self.name isEqualToString:@"所有作品"]) {
    
    DetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DetailsCollectionViewCell class]) forIndexPath:indexPath];
        
        [cell.photoImageView setImageWithURL:[NSURL URLWithString:self.hotArr[indexPath.row][@"vertical_image_url"]] placeholderImage:[UIImage imageNamed:@"fakeimg"]];
    
    cell.nameLabel.text = self.hotArr[indexPath.row][@"title"];
    
    cell.nameLabel.font = [UIFont boldSystemFontOfSize:16];
    
    cell.authorLabel.text = [[self.hotArr[indexPath.row]objectForKey:@"user"]valueForKey:@"nickname"];
    
    cell.authorLabel.font = [UIFont boldSystemFontOfSize:12];
    
    return cell;
        
    }else{
    
        ActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ActivityCollectionViewCell class]) forIndexPath:indexPath];
        
        [cell.activityImageView setImageWithURL:[NSURL URLWithString:self.comicsArr[indexPath.row][@"cover_image_url"]] placeholderImage:[UIImage imageNamed:@"fakeimg"]];
    
        cell.nameLabel.text = self.comicsArr[indexPath.row][@"title"];
        
        cell.nameLabel.font = [UIFont boldSystemFontOfSize:14];
    
        cell.titleName.text = [[[self.comicsArr[indexPath.row]objectForKey:@"topic"]objectForKey:@"user"]objectForKey:@"nickname"];
        
        cell.titleName.font = [UIFont boldSystemFontOfSize:12];
    
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *zero = [NSNumber numberWithFloat:0];
    NSString *hot;
    NSString *comics;
    if ([self.type isEqualToNumber:zero] || [self.name isEqualToString:@"所有作品"]) {
    hot =  [NSString stringWithFormat:@"%@",[self.hotArr[indexPath.row]objectForKey:@"id"]];
    }else{
    comics = [NSString stringWithFormat:@"%@",[self.comicsArr[indexPath.row]objectForKey:@"id"]];
    }
    
    
    NSLog(@"%@%@",hot,comics);

    if (hot.length == 0) {
          [[NSNotificationCenter defaultCenter] postNotificationName:@"xiaozaoren" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:comics,@"1",@"comics",@"2",nil]];
    }else if(comics.length == 0){
          [[NSNotificationCenter defaultCenter] postNotificationName:@"xiaozaoren" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:hot,@"1",@"hot",@"2",nil]];
        
    }
    
    
  
    NSLog(@"%@",indexPath);
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
